package com.example.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.TokenSign;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.domain.dto.UserDTO;
import com.example.domain.po.User;
import com.example.service.UserService;
import com.example.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.example.common.enums.ResultCodeEnum.USER_NOT_LOGIN;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    /*
    * 用户登录
    * */
    @PostMapping("doLogin")
    public Result doLogin(@RequestBody UserDTO userDTO) {
        Map<String, Object> userInfo =userService.login(userDTO);
        return Result.success(userInfo);
    }

    /*
     * 用户注册
     * */
    @PostMapping("register")
    public Result register(@RequestBody UserDTO userDTO) {
        userService.register(userDTO);
        return Result.success("注册成功");
    }

    // 查询用户登录状态
    @RequestMapping("isLogin")
    public Result isLogin() {
        boolean loginStatus = StpUtil.isLogin();
        if (loginStatus){
            return Result.success(StpUtil.isLogin()) ;
        }
        return Result.error(USER_NOT_LOGIN) ;
    }
    //用户登出
    @RequestMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }
    //获取所有用户信息
    @GetMapping("getAllUser")
    @SaCheckPermission("user:getAllUser")
    @SaCheckLogin
    public Result getAllUser() {
        List<User> list=userService.list();
        System.out.println(StpUtil.getLoginId());
        return  Result.success(list);
    }

    //管理员踢人下线
    @RequestMapping("kickout/{userId}")
    @SaCheckRole("ADMIN")
    public Result kickout(@PathVariable Long userId) {
        StpUtil.kickout(userId);
        return Result.success("已下线");
    }
    /**
     * 获取所有在线用户的列表
     */
    @GetMapping("getOnlineUsers")
    @SaCheckRole("ADMIN")
    public Result getOnlineUsers() {
        List<UserDTO> loginUsers=userService.getOnlineUsers();
        return Result.success(loginUsers);
    }

    /**
     * 管理员对指定账号解除封禁：
     */
    @GetMapping("untieDisable/{userId}")
    @SaCheckRole("ADMIN")
    public Result untieDisable(@PathVariable Long userId) {
        StpUtil.untieDisable(userId);
        return Result.success("已解除封禁");
    }
    /**
     * 管理员对指定账号进行封禁：
     */
    @GetMapping("disable/{userId}")
    @SaCheckRole("ADMIN")
    public Result disable(@PathVariable Long userId) {
        StpUtil.disable(userId, 86400);//86400为1天
        return Result.success("账号已封禁");
    }




}
